#include "StdAfx.h"
#include "cResourceManager.h"
#define GETLINE(format, ...) fgets(line, 255, f);sscanf(line, format, __VA_ARGS__);printf("%s",line);
cResourceManager* cResourceManager::m_instance = NULL;
cResourceManager::cResourceManager(void)
{
	m_textureCube = NULL;
	m_textures = NULL;
	m_models = NULL;
	m_shaders = NULL;
}

cResourceManager::~cResourceManager(void)
{
	if(m_textureCube)
		delete[] m_textureCube;
	if(m_textures)
		delete[]  m_textures;
	if(m_models)
		delete[] m_models;
	if(m_shaders)
	{
		if(	m_shaders->m_states)
			delete[] m_shaders->m_states;
		m_shaders->m_states = NULL;
		delete[] m_shaders;
	}



	m_textureCube = NULL;
	m_textures = NULL;
	m_models = NULL;
	m_shaders = NULL;
}

int cResourceManager::Init(const char *fileName)
{
	FILE *f;
	char line[256];
	int sumTotal; 
	char value[256],value1[256];
	f = fopen (fileName, "rt");  
	if(f == NULL)
		return 0;

	

	GETLINE("#2DTextures: %d",&sumTotal);
	m_numTexture = sumTotal;
	m_textures =  new cTexture[sumTotal];
	for(int i = 0; i < sumTotal; ++i)
	{
		GETLINE("ID %d", &m_textures[i].id);
		GETLINE("FILE %s", value);
		GETLINE("WRAP %s", value1);
		if(strcmp(value1,"CLAMP") == 0)
		{
			m_textures[i].m_State = GL_CLAMP_TO_EDGE;

		}else if(strcmp(value1,"REPEAT")==0)
		{
			m_textures[i].m_State = GL_REPEAT;

		}
		else
		{
			m_textures[i].m_State = GL_MIRRORED_REPEAT;
		}
		m_textures[i].Init(value);
	}
	fgets(line,255,f);	
	sumTotal = 0;
	GETLINE("#Models: %d", &sumTotal);
	m_numModel = sumTotal;
	m_models = new Models[sumTotal]();
	for(int i = 0; i< sumTotal; ++i)
	{
		char typemode[30];
		int idTexture;
		GETLINE("ID %d", &m_models[i].ID);
		GETLINE("FILE %s %s %d", value, typemode,&idTexture);
		 if(strcmp(typemode,"MAP")==0)
			m_models[i].Init(value,idTexture);
		 else
			m_models[i].Init(value);
	}
	fgets(line,255,f);	
	sumTotal = 0;

	GETLINE("#Cube Textures:%d",&sumTotal);
	m_numTextureCube = sumTotal;
	m_textureCube = new cTexturesCube[sumTotal];
	for(int i = 0; i < sumTotal; ++i)
	{
		GETLINE("ID %d", &m_textureCube[i].id);
		char listFile[6][255];
		for(int j = 0; j < 6; ++j)
		{
			GETLINE("FILE %s", listFile[j]);
		}
		m_textureCube[i].Init(listFile);
	}
	fgets(line,255,f);	
	sumTotal = 0;

	GETLINE("#Shaders: %d",&sumTotal);
	m_numShaders = sumTotal;
	m_shaders = new Shaders[sumTotal];
	for(int i = 0; i < sumTotal; ++i)
	{
		char nameVS[256],nameFS[256];
		int numShader;
		GETLINE("ID %d", &m_shaders[i].id);
		GETLINE("VS %s", &nameVS);
		GETLINE("FS %s", &nameFS);
		m_shaders[i].Init(nameVS, nameFS);
		GETLINE("STATES %d", &numShader);
		m_shaders[i].m_states = new int[numShader];
		m_shaders[i].m_numState = numShader;
		for(int j = 0; j < numShader;  ++j)
		{
			GETLINE("STATE %s",&value);
			if(strcmp(value, "CULLING")==0)
				m_shaders[i].m_states[j] = GL_CULL_FACE;
			else if(strcmp(value, "BLEND")==0)
				m_shaders[i].m_states[j] = GL_BLEND;
			else if(strcmp(value, "ALPHA")==0)
				m_shaders[i].m_states[j] = GL_ALPHA;
			else if(strcmp(value, "STENCIL")==0)
				m_shaders[i].m_states[j] = GL_STENCIL_TEST;
			else if(strcmp(value, "DEPTH_TEST")==0)
				m_shaders[i].m_states[j] = GL_DEPTH_TEST;
			else if(strcmp(value, "SCISSOR")==0)
				m_shaders[i].m_states[j] = GL_SCISSOR_TEST;
		}
	}
	fclose(f);	
	return 1;
}
Models*	cResourceManager::GetModel(int id)
{
	for(int i = 0; i < m_numModel; ++i)
	{
		if(m_models[i].ID == id)
			return &m_models[i];
	}
	return NULL;
}
	
cTexture*	cResourceManager::GetTexture(int id)
{
	for(int i = 0; i < m_numTexture; ++i)
	{
		if(m_textures[i].id == id)
			return &m_textures[i];
	}
	return NULL;
}
	
cTexturesCube*	cResourceManager::GetTextureCube(int id)
{
	for(int i = 0; i < m_numTextureCube; ++i)
	{
		if(m_textureCube[i].id == id)
			return &m_textureCube[i];
	}
	return NULL;
}

Shaders*	cResourceManager::GetShader(int id)
{
	for(int i = 0; i < m_numShaders; ++i)
	{
		if(m_shaders[i].id == id)
			return &m_shaders[i];
	}
	return NULL;
}
cResourceManager* cResourceManager::GetInstance()
{
	if(m_instance == NULL)
	{
		m_instance = new cResourceManager();
	}
	return m_instance;
	//return NULL;
}
void cResourceManager::destroy()
{
	if ( m_instance )
		{
			delete m_instance;
		
		}
			m_instance = NULL;
}
